package com.niit.schoolhub.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.niit.schoolhub.system.dto.ApiJson;
import com.niit.schoolhub.system.mapper.YesapiBjyOauthUserMapper;
import com.niit.schoolhub.system.mapper.YesapiOneUsersMapper;
import com.niit.schoolhub.system.pojo.YesapiBjyOauthUser;
import com.niit.schoolhub.system.pojo.YesapiOneUsers;
import com.niit.schoolhub.system.utils.JwtFormat;
import com.niit.schoolhub.system.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 轻量博客-用户 前端控制器
 * </p>
 *
 * @author kaguya
 * @since 2021-11-27
 */
@RestController
@CrossOrigin
@RequestMapping("/system/yesapiOneUsers")
public class YesapiOneUsersController {
    @Autowired
    private YesapiOneUsersMapper yesapiOneUsersMapper;

    @Autowired
    private YesapiBjyOauthUserMapper yesapiBjyOauthUserMapper;

    private ResultCode resultCode;
@PostMapping("/userregister")
public int useregister(@RequestBody YesapiOneUsers yesapiOneUsers){
    LocalDateTime localDateTime = LocalDateTime.now();
    yesapiOneUsers.setLastLoginTime(localDateTime);
    System.out.println(yesapiOneUsers);
    List<YesapiOneUsers> yesapiOneUsers1 = yesapiOneUsersMapper.selectList(null);
    for (YesapiOneUsers user:yesapiOneUsers1
         ) {
        if (yesapiOneUsers.getUsername()==user.getUsername()){
            return 2;
        }
    }
    int i=yesapiOneUsersMapper.insert(yesapiOneUsers);
    return i;
    }


    @PostMapping("/userlogin")
    public YesapiBjyOauthUser userlogin(@RequestBody YesapiOneUsers yesapiOneUsers){
        LocalDateTime now = LocalDateTime.now();
        QueryWrapper<YesapiOneUsers> QueryWrapper = new QueryWrapper<>();
        HashMap<String, String> map = new HashMap<>();
        map.put("username",yesapiOneUsers.getUsername());
        map.put("yesapi_one_users_password",yesapiOneUsers.getYesapiOneUsersPassword());
        QueryWrapper.allEq(map);
        List<YesapiOneUsers> yesapiOneUsers1 = yesapiOneUsersMapper.selectList(QueryWrapper);
        if (yesapiOneUsers1.size()==1){
            yesapiOneUsers1.get(0).setLastLoginTime(now);
            yesapiOneUsersMapper.updateById(yesapiOneUsers1.get(0));
            YesapiBjyOauthUser yesapiBjyOauthUser = yesapiBjyOauthUserMapper.selectById(yesapiOneUsers1.get(0).getId());
//            System.out.println(yesapiBjyOauthUser);
            yesapiBjyOauthUser.setAccessToken(JwtFormat.generateToken(yesapiBjyOauthUser));
//            System.out.println(yesapiBjyOauthUser.getAccessToken());
            return yesapiBjyOauthUser;
        }
        return null;
    }

    @GetMapping("/user/unlogin")
    public void unlogin(String msg) throws UnsupportedEncodingException {
        System.out.println(new String(msg.getBytes(), "UTF-8"));
    }

    @PostMapping("/getuser")
    public List<YesapiOneUsers> getUser(@RequestBody YesapiOneUsers yesapiOneUsers){
        QueryWrapper<YesapiOneUsers> QueryWrapper = new QueryWrapper<>();
        HashMap<String, String> map = new HashMap<>();
        map.put("username",yesapiOneUsers.getUsername());
        map.put("yesapi_one_users_password",yesapiOneUsers.getYesapiOneUsersPassword());
        QueryWrapper.allEq(map);
        List<YesapiOneUsers> yesapiOneUsers1 = yesapiOneUsersMapper.selectList(QueryWrapper);
        return  yesapiOneUsers1;
    }

}

